<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-inbound-add">
			<div class="form-group">	
				<label class="col-sm-3 control-label">入库日期：</label>
				<div class="col-sm-8">
					<input id="inboundDate" name="inboundDate" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">物品名称：</label>
				<div class="col-sm-8">
					<select id="goodsId" name="goodsId" class="form-control select2-hidden-accessible" >
						<option th:each="goodsList:${goodsLists}" th:value="${goodsList.goodsId}" th:text="${goodsList.goodsName}" ></option>
					</select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">数量：</label>
				<div class="col-sm-8">
					<input id="inboundNumber" name="inboundNumber" class="form-control" type="text" onkeyup="countMoney()">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">单价：</label>
				<div class="col-sm-8">
					<input id="inboundPrice" name="inboundPrice" class="form-control" type="text" onkeyup="countMoney()">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">金额：</label>
				<div class="col-sm-8">
					<input id="inboundMoney" name="inboundMoney" class="form-control" type="text" readonly="readonly">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<div class="form-control-static col-sm-offset-9">
					<button type="submit" class="btn btn-primary">提交</button>
					<button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script th:src="@{/ajax/libs/select/select2.js}"></script>
    <script type="text/javascript">
	    layui.use('laydate', function(){
			var laydate = layui.laydate;
			//执行一个laydate实例
			laydate.render({
				elem: '#inboundDate' //指定元素
			});
		});
	    
	    Date.prototype.Format = function (fmt) { 
	        var o = {
	            "M+": this.getMonth() + 1, //月份 
	            "d+": this.getDate(), //日 
	            "h+": this.getHours(), //小时 
	            "m+": this.getMinutes(), //分 
	            "s+": this.getSeconds(), //秒 
	            "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
	            "S": this.getMilliseconds() //毫秒 
	        };
	        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	        for (var k in o)
	        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	        	return fmt;
    	} 
    
		$("#form-inbound-add").validate({
			rules:{
				inboundDate:{
					required:true,
				},
				inboundNumber:{
					required:true,
				},
			},
			submitHandler: function(form) {
				add();
			}
		});
		
		function add() {
        	var inboundDate = $("input[name='inboundDate']").val();
        	inboundDate = new Date(inboundDate).Format("yyyy-MM-dd hh:mm:ss");
        	var goodsId = $.form.selectSelects("goodsId");
        	var inboundNumber = $("input[name='inboundNumber']").val();
        	var inboundPrice = $("input[name='inboundPrice']").val();
        	var inboundMoney = $("input[name='inboundMoney']").val();
        	var remark =  $("input[name='remark']").val();
        	$.ajax({
        		cache : true,
        		type : "POST",
        		url : ctx + "store/inbound/add",
        		data : {
        			"inboundDate": inboundDate,
        			"goodsId": goodsId,
        			"inboundNumber" : inboundNumber,
        			"inboundPrice" : inboundPrice,
        			"inboundMoney" : inboundMoney,
        			"remark":remark
        		},
        		async : false,
        		error : function(request) {
        			$.modal.alertError("系统错误");
        		},
        		success : function(data) {
        			$.operate.saveSuccess(data);
        		}
        	});
        }
		
		function countMoney(){
        	var inboundPrice = $("input[name='inboundPrice']").val();
        	var inboundNumber = $("input[name='inboundNumber']").val();
        	var accountMoneyInput =  document.getElementById("inboundMoney");
        	accountMoneyInput.value = inboundPrice*inboundNumber;
        	
        }
		
	</script>
</body>
</html>
